<?php
/**
 * Created by PhpStorm.
 * User: wangchaozhen
 * Date: 2018/5/6
 * Time: 14:10
 */
namespace App\Common;

class Fun
{
    /**
     * 1网易请求
     * curl请求
     */
    public static function sendUrl($method = 'post', $url = '',$param = [],$type = 0)
    {
        $ch = curl_init();

        if($method == 'get'){
            curl_setopt($ch,CURLOPT_URL,$url.'?'.http_build_query($param));
        }else{
            curl_setopt($ch,CURLOPT_URL,$url);
        }

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //不验证证书
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);
            $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
            $param = json_encode($param);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        if($type=='xml'){
            $headers = array("Content-type: text/xml");
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        }

        if($method == 'post'){
            curl_setopt($ch,CURLOPT_POST,1);
            if($type == 1){
                curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($param));
            }else{
                curl_setopt($ch,CURLOPT_POSTFIELDS,$param);
            }
        }

        $result = curl_exec($ch);

        curl_close($ch);
        return $result;
    }
}